import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import { exec } from 'child_process';
import { promisify } from 'util';

const execAsync = promisify(exec);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const projectsPath = path.join(__dirname, '..', 'projects');

async function installDependencies() {
    try {
        const projects = fs.readdirSync(projectsPath).filter(file => {
            return fs.statSync(path.join(projectsPath, file)).isDirectory();
        });

        for (const project of projects) {
            const projectPath = path.join(projectsPath, project);
            console.log(`正在安装 ${project} 的依赖...`);
            const { stdout, stderr } = await execAsync('npm install', { cwd: projectPath });
            console.log(stdout);
            if (stderr) console.error(stderr);
        }

        console.log('所有项目依赖安装完成！');
    } catch (error) {
        console.error('安装过程出错：', error);
    }
}

installDependencies();